﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceProcess;
using Tree.Runner;
using Tree.Injector;
using Tree.Container;
using Tree.Events;
using Tree.Runner.Events;
using Tree.Events.Impl;
using System.Threading;
using log4net;
using System.Reflection;

namespace Tree.Runner
{
    public class WrappedService : ServiceBase
    {
        private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        [Inject()]
        private IEventsManager eventsManager;

        protected override void OnStart(string[] args)
        {
            Core.Container.Start();
            eventsManager.Publish(ServiceStartEventArgs.NAME, null);
            logger.Debug("Service Started");
        }

        protected override void OnStop()
        {
            Core.Container.Stop();
            eventsManager.Publish(ServiceStopEventArgs.NAME, null);
            logger.Debug("Service Stopped");
        }

        public WrappedService()
        {
            CanHandleSessionChangeEvent = true;
        }

        protected override void OnSessionChange(SessionChangeDescription changeDescription)
        {
            eventsManager.Publish(SessionChangeEventArgs.NAME, new SessionChangeEventArgs(changeDescription));
        }
    }
}
